//
//  XQMeController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit
import MJRefresh

class XQMeController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    private var meModel : XQMeModel?
    
    private lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        
        scrollView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.refreshData()
        })
        
        return scrollView
    }()
    
    private lazy var mainView : UIView = {
        let v = UIView()
        return v
    }()
    
    private lazy var topImageView : UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "me_head_img")
        return imageView
    }()
    
    private lazy var avatar : UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "usr_photo")
        return imageView
    }()
    
    private lazy var identifyLb : UILabel = {
       let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 21,weight: .medium)
        return lb
    }()
    
    private lazy var tableView : UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.register(XQMeCell.self, forCellReuseIdentifier: "XQMeCell")
        
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        makeUI()
        makeConstraints()
        
        XQHud.showLoading(controller: self)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        refreshData()
    }
    
    private func makeUI() {
        self.view.addSubview(self.scrollView)
        self.scrollView.addSubview(self.mainView)
        self.mainView.addSubview(self.topImageView)
        self.topImageView.addSubview(self.avatar)
        self.topImageView.addSubview(self.identifyLb)
        self.mainView.addSubview(self.tableView)
    }
    
    private func makeConstraints() {
        
        self.scrollView.snp.makeConstraints { make in
            make.top.equalTo(UIDevice.xq_statusHeight() + 24)
            make.left.right.equalTo(0)
            make.bottom.equalTo(-UIDevice.xq_bottomSafeMargin() - 48)
        }
        
        self.mainView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(UIDevice.xq_width())
        }
        
        self.topImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(222)
        }
        self.avatar.snp.makeConstraints { make in
            make.top.equalTo(45)
            make.left.equalTo(35)
            make.height.width.equalTo(108)
        }
        self.identifyLb.snp.makeConstraints { make in
            make.top.equalTo(69)
            make.left.equalTo(self.avatar.snp.right).offset(12)
        }
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.topImageView.snp.bottom).offset(10)
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(0)
        }
    }
    
    private func refreshData() {
        XQNetworkManager.shared.getRequest(url: "/figures/repeatable") {[weak self] response, error in
            guard let srongify = self else {return}
            srongify.scrollView.mj_header?.endRefreshing()
            XQHud.hideLoading(controller: srongify)
            if (response?.object == 0) {
                if let model = XQMeModel.deserialize(from: response?.procedure) {
                    srongify.meModel = model
                    srongify.updatePageData()
                }
            }
        }
    }
    
    private func updatePageData() {
        self.identifyLb.text = meModel?.userInfo?.userphone ?? ""
        
        if let array : [XQMeReadingsModel] = meModel?.readings {
            if (array.count != 0) {
                self.tableView.snp.updateConstraints { make in
                    make.height.equalTo(80 * array.count)
                }
            }
            self.tableView.reloadData()
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return meModel?.readings?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XQMeCell", for: indexPath) as! XQMeCell
        if (indexPath.row < meModel?.readings?.count ?? 0) {
            cell.model = meModel?.readings![indexPath.row]
        }
        cell.isLast = (meModel?.readings?.count ?? 0) - 1 == indexPath.row
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (indexPath.row < meModel?.readings?.count ?? 0) {
            let model : XQMeReadingsModel? = meModel?.readings?[indexPath.row]
            if let url = model?.measured {
                XQRouteTool.jumpController(controller: self, url: url)
            }
        }
    }

}
